<?php
class ModuleSaveEditAction extends AdminAction
{
	public function __construct()
	{
		parent::__construct();
		$this->responseType = 'json';
	}
	
	protected function afterConstruct()
	{
		$this->parseDtds('module','edit');
	}
	
	public function execute()
	{
		$id		= $this->context->post('id');
		$data	= $this->getValidData();
		
		//数据更新时间24小时
		$data['update_period'] = 24;
		
		$moduleService = new BaseDbService('module');
		
		//标签
		$data['tags'] = str_replace("，", ",", $data['tags']);
		$tmp = explode(',', $data['tags']);
		if(count($tmp)>5)
		{
			$this->data['error_messages']['tags']	= array('error'=>'最多只能添加五个标签');
		}
		
		if(empty($this->data['error_messages']))
		{
			if(is_numeric($id) && $id>0)
			{
				$data['data'] = '';
				$moduleService->update($id,$data);
				$this->code	= 200;
			}
			else
			{
				$id	= $moduleService->add($data);
				if($id)
				{
					$this->code	= 200;
				}
				else
				{
					$this->code	= 400;
				}
			}
		}
		else
		{
			$this->code	= 400;
		}
		
		$this->data['forward'] = C('LtUrl')->generate('Module', 'Index',array('position'=>$data['position']));
	}
}
